package org.hitchhike.web.front.controller;

import java.util.Date;

import org.hitchhike.common.web.controller.BaseController;
import org.hitchhike.core.domain.Comment;
import org.hitchhike.core.domain.Order;
import org.hitchhike.core.domain.User;
import org.hitchhike.core.enums.Constant;
import org.hitchhike.core.exception.ErrorInfo;
import org.hitchhike.core.form.CommentForm;
import org.hitchhike.core.service.CommentService;
import org.hitchhike.core.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value = "/front/comment")
public class CommentController extends BaseController<Comment, Long> {
  private Logger logger = LoggerFactory.getLogger(getClass());

  @Autowired
  private CommentService commentService;
  @Autowired
  private OrderService orderService;

  @RequestMapping(value = "", method = RequestMethod.GET)
  public String driver(Model model, Integer page) {
    model.addAttribute("page", commentService.findPageComment(page));
    model.addAttribute("nav_active", "comment");
    return viewName("/comment");
  }

  @RequestMapping(value = "/save", method = RequestMethod.POST)
  public String save(CommentForm commentForm, RedirectAttributes redirectAttributes) {
    ErrorInfo<String> r = new ErrorInfo<>();
    User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    Comment comment = new Comment();
    comment.setCommentContent(commentForm.getCommentContent());
    comment.setOrder(new Order(commentForm.getOrderId()));
    comment.setCommentTime(new Date());
    comment.setUser(user);

    try {
      commentService.save(comment);

      Order order = orderService.get(commentForm.getOrderId());
      if (order.getOrderProcess() == Constant.ORDER_PROCESS_3) {
        order.setOrderProcess(Constant.ORDER_PROCESS_4);
        orderService.update(order);
      }
      r.setCode(ErrorInfo.OK);
      r.setMessage(ErrorInfo.MESSAGE_OK);
    } catch (Exception e) {
      logger.error(e.getMessage());
      r.setCode(ErrorInfo.ERROR);
      r.setMessage(ErrorInfo.MESSAGE_ERROR);
    }

    redirectAttributes.addFlashAttribute("r", r);
    return redirectToUrl("/front/order/reserve/comment/" + commentForm.getOrderId());
  }

  @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
  public String delete(@PathVariable Long id, RedirectAttributes redirectAttributes) {
    ErrorInfo<String> r = new ErrorInfo<>();
    try {
      commentService.delete(id);

      r.setCode(ErrorInfo.OK);
      r.setMessage(ErrorInfo.MESSAGE_OK);
    } catch (Exception e) {
      logger.error(e.getMessage());
      r.setCode(ErrorInfo.ERROR);
      r.setMessage(ErrorInfo.MESSAGE_ERROR);
    }
    redirectAttributes.addFlashAttribute("r", r);
    return redirectToUrl("");
  }

}
